import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/api.dart';
import 'package:guochaoapp/common/utils/http_client.dart';
import 'package:guochaoapp/common/widges/dialog.dart';
import 'package:guochaoapp/controller/mine/mine_controller.dart';
import 'package:guochaoapp/model/address_entity.dart';
import 'package:guochaoapp/model/cart_settle_entity.dart';
import 'package:guochaoapp/model/default_address_entity.dart';
import 'package:guochaoapp/model/invoice_entity.dart';
import 'package:guochaoapp/model/order_detail_entity.dart';
import 'package:guochaoapp/routes/app_pages.dart';
import 'package:guochaoapp/services/user_service.dart';
import 'package:guochaoapp/view/mine/address_page.dart';

import '../../r.dart';
import '../tabbar_controller.dart';
import 'order_detail_controller.dart';

class GCOrderInvoiceController extends GetxController {
  UserService userService = Get.find();

  var invoiceData = InvoiceData().obs;

  var textEditingControllers = [
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
  ].obs;

  var titles = [
    "单位名称",
    "纳税人识别码",
    "注册地址",
    "注册电话",
    "开户银行",
    "银行账号",
    "收票姓名",
    "收票电话",
    "收票地址"
  ];

  void requestInvoiceData() {
    HttpClient.request<InvoiceEntity>(
        url: UserInvoiceApi.usersInvoice,
        method: Method.GET,
        params: {"uid": userService.userInfo.uid},
        onSuccess: (_, model) {
          if (model.data != null) {
            invoiceData.value = model.data;
            textEditingControllers[0].text = invoiceData.value.companyName;
            textEditingControllers[1].text = invoiceData.value.taxpayerIdNum;
            textEditingControllers[2].text = invoiceData.value.registerAddr;
            textEditingControllers[3].text = invoiceData.value.registerPhone;
            textEditingControllers[4].text = invoiceData.value.branchName;
            textEditingControllers[5].text = invoiceData.value.cardNo;
            textEditingControllers[6].text = invoiceData.value.receiverName;
            textEditingControllers[7].text = invoiceData.value.receiverPhone;
            textEditingControllers[8].text = invoiceData.value.receiverAddr;
          }
        },
        onFailed: (err) {
          EasyLoading.showError(err);
        });
  }

  void submitInvoice(String mallOrderNo) {
    showDialog(
        context: Get.context,
        barrierDismissible: false,
        builder: (ctx) {
          return GCDialog(
            items: ['取消', '继续申请'],
            content: '申请开具发票后将无法发起退货\n请确保订单已完成',
            title: Image.asset(R.assetsIconWarning, height: 27, width: 27),
            onTap: (_) {
              HttpClient.request(
                  url: MallApi.invoiceOrder,
                  method: Method.POST,
                  data: {
                    "companyName": textEditingControllers[0].text,
                    "taxpayerIdNum": textEditingControllers[1].text,
                    "registerAddr": textEditingControllers[2].text,
                    "registerPhone": textEditingControllers[3].text,
                    "branchName": textEditingControllers[4].text,
                    "cardNo": textEditingControllers[5].text,
                    "receiverName": textEditingControllers[6].text,
                    "receiverPhone": textEditingControllers[7].text,
                    "receiverAddr": textEditingControllers[8].text,
                    "uid" : userService.userInfo.uid,
                    "mallOrderNo": mallOrderNo
                  },
                  onSuccess: (r, _) {
                    Get.back();
                    GCOrderDetailController c = Get.find();
                    c.requestInitData(mallOrderNo);
                  },
                  onFailed: (err) {
                    EasyLoading.showError(err);
                  }
              );
            },
          );
        });
  }

}
